//Hung Nguyen 
//Jeremy Sardoma
//Matthew Alcazar
#include "ship.h"



Ship::Ship() {
	Pos.x=0;
	Pos.y=0;
	Pos.z=0;
	slices=50;
	stacks=10;
	rings=50;
	angleVertical=0;
	angleHorizontal=0;
	angleAt=0;
	gravity = false;	
	turbo = false;
	modelListId=1;
	hidden = false;
}
Ship::Ship(float a, float b, float c) {
	Pos.x=a;
	Pos.y=b;
	Pos.z=c;
	slices=50;
	stacks=10;
	rings=50;
	angleVertical=0;
	angleHorizontal=0;
	angleAt=0;
	gravity = false;
	turbo = false;
	modelListId=1;
	hidden = false;
}
void Ship::Draw() {	
	//Calculate Gravity
	if(gravity==true)
	{
		Vector mGravityVector = Vector(-Pos.x,-Pos.y,-Pos.z);
		float GV = 32000000/(mGravityVector.magnitude()*mGravityVector.magnitude());
		printf("\n GV is : %f", GV);
		if(Pos.x>0) Pos.x= Pos.x-GV; else Pos.x= Pos.x+GV;
		if(Pos.y>0) Pos.y= Pos.y-GV; else Pos.y= Pos.y+GV;
		if(Pos.z>0) Pos.z= Pos.z-GV; else Pos.z= Pos.z+GV;
	}
	glPushMatrix(); //set where to start the current object transformations	
	//Translate
	glTranslatef(Pos.x,Pos.y,Pos.z);	
	//glRotatef(angleAt,0,0,1);	
	glRotatef(angleHorizontal,mRotationAxis.x,mRotationAxis.y,mRotationAxis.z);	
	glRotatef(angleVertical,1,0,0);	
	//Draw Ship
	/*glColor3f(0, 1.0, 0.0);
	glutSolidCone(60,160,slices,rings);
	glColor3f(0, 1.0, 1.0);
	glTranslatef(0,30,40);
	glutSolidSphere(40,slices,stacks);	*/
	//glRotatef(-45,1,0,0);
	if(hidden == false){
		glCallList(modelListId);   // draw model's display list 
	}
	glutSwapBuffers();
	glPopMatrix(); //end the current object transformations
}

//void Ship::Forward(GLfloat Distance) {
//	
//}

//void Ship::RotateToward(Vector target){
//	GLfloat m[16];
//	glGetFloatv(GL_MODELVIEW_MATRIX, m);
//	Vector at = Vector(m[8],m[9],m[10]);
//	Vector displacement = Pos.subtract(target);
//	Vector rotateAxis = at.cross(displacement);
//	float dot = at.dot(displacement);
//	angleHorizontal = acos(dot/(at.magnitude()*displacement.magnitude()));
//	mRotationAxis = rotateAxis;
//}
 void Ship::MoveForward(int distance){
		if(turbo==true) distance = distance * 4;
		Pos.y+=(distance*sin(-(angleVertical*2*3.141592)/360));		 
		 if(angleVertical<=90 && angleVertical>=-90)
		 {
			 Pos.z+=(distance*cos((angleHorizontal*2*3.141592)/360));
			Pos.x+=(distance*sin((angleHorizontal*2*3.141592)/360));
		 }
		 else
		 {
			Pos.z+=-(distance*cos((angleHorizontal*2*3.141592)/360));
			Pos.x+=-(distance*sin((angleHorizontal*2*3.141592)/360));
		 }
		 printf("\nangle is  %f \n",angleHorizontal);
		 printf("Vertical Angle is %f \n",angleVertical);
		 printf("delta x is %f \n",(distance*sin((angleHorizontal*2*3.141592)/360)));
		 printf("delta z is %f \n",(distance*cos((angleHorizontal*2*3.141592)/360)));
		 printf("delta y is %f \n",(distance*sin(-(angleVertical*2*3.141592)/360)));
 }

 void Ship::RotateHorizontal(GLfloat AdditionAngle){
	 angleHorizontal+=AdditionAngle;
	 if(angleHorizontal>180) angleHorizontal=angleHorizontal-360;
	 if(angleHorizontal<-180) angleHorizontal = angleHorizontal+360;
	 //mRotationAxis= rotationaxis;
 }

  void Ship::RotateVertical(GLfloat AdditionAngle){
	 angleVertical+=AdditionAngle;
	 if(angleVertical>180) angleVertical=angleVertical-360;
	 if(angleVertical<-180) angleVertical = angleVertical+360;
	 //mRotationAxis= rotationaxis;
 }
   void Ship::RotateAt(GLfloat AdditionAngle){
	 angleAt+=AdditionAngle;
	 if(angleAt>180) angleAt=angleAt-360;
	 if(angleAt<-180) angleAt = angleAt+360;
	 //mRotationAxis= rotationaxis;
 }